import 'package:flutter/material.dart';
import '../page/Tabs.dart';
import '../page/NewsPage.dart';
import '../page/NewsPage1.dart';
import '../page/NewsPage2.dart';

final routers = {
  '/': (context) => Tabs(),
  '/news': (context) => NewsPage(),
  '/news1': (context, {arguments}) => NewsPage1(arguments),
  //区别只在于一个可选，一个必选，大括号包着的代表构造函数的arguments是可选的
  '/news2': (context, {arguments}) => NewsPage2(arguments: arguments),
  //错误的写法，这样子实际是传输了一个key和value都为arguments的对象
  '/news2': (context, {arguments}) => NewsPage1({arguments: arguments}),
};

var onGenerateRoute = (RouteSettings settings) {
  final String name = settings.name;

  //查看源码得知Functino会动态调用它的参数
  final Function pageContentBuilder = routers[name];

  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(
          builder: (context) =>
          //动态调用构造器
          //这种方法只传了一个参数，路由需要调用push(context, '/news', RouteSetting(...);
//          pageContentBuilder(context));

          //这种方法对news1, news2
              pageContentBuilder(context, arguments: settings.arguments));

      return route;
    } else {
      final Route route =
          MaterialPageRoute(builder: (context) => pageContentBuilder(context));
      return route;
    }
  }
  return null;
};
